﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace SmallJob.Services.Code
{
    public class UploadHelper
    {
        public static String GetRootPath()
        {
            return System.Web.HttpContext.Current.Server.MapPath("~");
        }
        public static String GetPathSaveFile(int uploaderid)
        {
            String foldername = "../Files/RarZip";
            var file = Path.GetFullPath(Path.Combine(GetRootPath(), foldername));
            if (!Directory.Exists(file))
            {
                Directory.CreateDirectory(file);
            }
            var specificfile = file + "/" + uploaderid;
            if (!Directory.Exists(specificfile))
            {
                Directory.CreateDirectory(specificfile);
            }
            return Path.GetFullPath(Path.Combine(GetRootPath(), foldername + "/" + uploaderid + "/"));
        }
        public static String GetNamePlusDatetime(String fileName, bool rmvNotAllowCharacter)
        {
            var filename = fileName;
            if (rmvNotAllowCharacter)
            {
                filename = RemoveNotAllowCharacter(filename);
            }
            return Path.GetFileNameWithoutExtension(filename) + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + Path.GetExtension(filename);
        }
        public static String RemoveNotAllowCharacter(String filename)
        {
            //Regex regex = new Regex(Resources.Resource.RegexRemoveCharacter);
            Regex regex = new Regex("[+&]");
            return regex.Replace(filename, "");
        }


        public static string GetLinkDownload(int ownerid, string filename, int downloader, int orderid)
        {
            var temp = UserUtility.HashCode(ownerid + filename + downloader + orderid);
            temp = HttpUtility.UrlEncode(temp);
            return "/File/Download?code=" + temp + "&id=" + orderid + "&filename=" + filename;
            //return "/File/Download?orderid=" + orderid + "&ownerid=" + ownerid + "&filename=" + filename;
        }
    }
}
